package com.tronsis.distribution.util;

import android.content.Context;
import android.text.TextUtils;

import com.lzy.imagepicker.bean.ImageItem;
import com.tronsis.distribution.SysApplication;
import com.tronsis.distribution.bean.RegionBean;
import com.tronsis.distribution.bean.RulesMapBean;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class CommonUtil {

    private static final String TAG = CommonUtil.class.getSimpleName();


    public static ArrayList<ImageItem> getSplitImage(String fids) {
        ArrayList<ImageItem> imageList = new ArrayList<>();
        if (!TextUtils.isEmpty(fids)) {
            String[] strings = fids.split(",");
            for (String string : strings) {
                ImageItem imageItem = new ImageItem(string, System.currentTimeMillis());
                imageItem.url = Url.FILE_DOWNLOAD + string;
                imageList.add(imageItem);
            }
        }

        return imageList;
    }

    public static String getRegionString(String code) {
        if (TextUtils.isEmpty(code)) {
            LogUtil.e(TAG, "错误的邮政编码");
            return "";
        }

        StringBuilder sb = new StringBuilder();
        try {
            List<RegionBean> provinceList = SysApplication.getRegionList();
            for (RegionBean province : provinceList) {
                //省份
                if (code.startsWith(province.getCode())) {
                    sb.append(province.getName());
                    if (StringUtil.isEquals(code, province.getCode())) {
                        break;
                    }
                    List<RegionBean> cityList = province.getChildren();
                    for (RegionBean city : cityList) {
                        //市区
                        if (code.startsWith(city.getCode())) {
                            sb.append(city.getName());
                            if (StringUtil.isEquals(code, city.getCode())) {
                                break;
                            }
                            List<RegionBean> regionList = city.getChildren();

                            for (RegionBean region : regionList) {
                                //县
                                if (code.startsWith(region.getCode())) {
                                    sb.append(region.getName());
                                    if (StringUtil.isEquals(code, region.getCode())) {
                                        break;
                                    }
                                    List<RegionBean> townshipList = region.getChildren();

                                    for (RegionBean township : townshipList) {
                                        if (code.startsWith(region.getCode())) {
                                            sb.append(township.getName());
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                            break;
                        }


                    }
                    break;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        LogUtil.e(TAG, "邮政编码:" + code + " -> " + sb.toString());
        return sb.toString();
    }


    public static String getCityRegionString(String code) {
        if (TextUtils.isEmpty(code)) {
            LogUtil.e(TAG, "错误的邮政编码");
            return "";
        }

        StringBuilder sb = new StringBuilder();
        try {
            List<RegionBean> provinceList = SysApplication.getRegionList();
            for (RegionBean province : provinceList) {
                //省份
                if (code.startsWith(province.getCode())) {
                    if (StringUtil.isEquals(code, province.getCode())) {
                        break;
                    }
                    List<RegionBean> cityList = province.getChildren();
                    for (RegionBean city : cityList) {
                        //市区
                        if (code.startsWith(city.getCode())) {
                            sb.append(city.getName());
                            if (StringUtil.isEquals(code, city.getCode())) {
                                break;
                            }
                            List<RegionBean> regionList = city.getChildren();

                            for (RegionBean region : regionList) {
                                //县
                                if (code.startsWith(region.getCode())) {
                                    sb.append(region.getName());
                                    if (StringUtil.isEquals(code, region.getCode())) {
                                        break;
                                    }
                                }
                            }
                            break;
                        }


                    }
                    break;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        LogUtil.e(TAG, "邮政编码:" + code + " -> " + sb.toString());
        return sb.toString();
    }


    public static RegionBean getProvinceRegion(String code) {
        if (TextUtils.isEmpty(code)) {
            LogUtil.e(TAG, "错误的邮政编码");
            return null;
        }
        RegionBean regionBean = null;
        try {
            List<RegionBean> provinceList = SysApplication.getRegionList();
            for (RegionBean province : provinceList) {
                //省份
                if (code.startsWith(province.getCode())) {
                    regionBean = province;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return regionBean;
    }


    public static RegionBean getCityRegion(String code) {
        if (TextUtils.isEmpty(code)) {
            LogUtil.e(TAG, "错误的邮政编码");
            return null;
        }
        RegionBean regionBean = null;
        try {
            List<RegionBean> provinceList = SysApplication.getRegionList();
            for (RegionBean province : provinceList) {
                //省份
                if (code.startsWith(province.getCode())) {
                    List<RegionBean> cityList = province.getChildren();
                    for (RegionBean city : cityList) {
                        if (StringUtil.isEquals(city.getCode(), code)) {
                            regionBean = city;
                            break;
                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return regionBean;
    }

    public static RegionBean getCountryRegion(String code) {
        if (TextUtils.isEmpty(code)) {
            LogUtil.e(TAG, "错误的邮政编码");
            return null;
        }
        RegionBean regionBean = null;
        try {
            List<RegionBean> provinceList = SysApplication.getRegionList();
            for (RegionBean province : provinceList) {
                //省份
                if (code.startsWith(province.getCode())) {
                    List<RegionBean> cityList = province.getChildren();
                    for (RegionBean city : cityList) {
                        if (code.startsWith(city.getCode())) {
                            List<RegionBean> countryList = city.getChildren();
                            for (RegionBean country : countryList) {
                                if (StringUtil.isEquals(country.getCode(), code)) {
                                    regionBean = country;
                                    break;
                                }
                            }

                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return regionBean;
    }


    public static RulesMapBean getRulesMap(Context context) {
        return DBUtil.getInstance(context).queryById(1, RulesMapBean.class);
    }


}
